package com.fyzk.utils.update;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.fyzk.activity.app.ActivityTaskManager;
import com.fyzk.activity.app.AppManager;
import com.fyzk.data.AppLotteryInfos;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.view.ViewCallBack;
import com.tyjc.lottery.R;


/**
 * app更新dialog
 * Created by Administrator on 2017/6/14 0014.
 */
public class UpdateDialog extends AlertDialog {

    private TextView newVersion, currentVersion, reminder;
    private String msgNewVersion, msgCurrentVersion, msgReminder;

    private UpdateClickInterface listenter;

    public interface UpdateClickInterface{
        void clickView(int type);
    }

    public void setUpdateListenter(UpdateClickInterface listenter) {
        this.listenter = listenter;
    }

    protected UpdateDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_update);
        this.getWindow().setGravity(Gravity.CENTER);
        initViews();
    }

    /**
     * 设置新版本信息
     * @param msg
     * @return
     */
    public UpdateDialog setNewVersion(String msg){
        this.msgNewVersion = msg;
        return  this;
    }

    /**
     * 设置当前版本信息
     * @param msg
     * @return
     */
    public UpdateDialog setCurrentVersion(String msg){
        this.msgCurrentVersion = msg;
        return  this;
    }

    /**
     * 设置更新内容
     * @param msg
     * @return
     */
    public UpdateDialog setReminder(String msg){
        this.msgReminder = msg;
        return  this;
    }

    private void initViews() {
        newVersion = (TextView) findViewById(R.id.newversion);
        newVersion.setText(msgNewVersion);
        currentVersion = (TextView) findViewById(R.id.currentversion);
        currentVersion.setText(msgCurrentVersion);
        reminder = (TextView) findViewById(R.id.reminder);
        reminder.setText(msgReminder);

        findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listenter != null)
                    listenter.clickView(1);
            }
        });

        findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listenter != null)
                    listenter.clickView(2);
            }
        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dismiss();
            AppLotteryInfos.clear();
            ViewCallBack.getInstance().countDownObjs.clear();
            ActivityTaskManager.getInstance().closeAllActivity();
            System.exit(0);
        }
        return super.onKeyDown(keyCode, event);
    }
}
